package com.bincloud.vue.admin.web.controller.ums;


import com.alibaba.fastjson.JSON;
import com.bincloud.vue.admin.common.model.dto.response.BasePageResDTO;
import com.bincloud.vue.admin.common.model.dto.response.BaseResDTO;
import com.bincloud.vue.admin.common.util.ResponseUtils;
import com.bincloud.vue.admin.web.dto.ums.resource.*;
import com.bincloud.vue.admin.web.service.ums.IUmsResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bincloud, mvpzhou
 * @since 2022-02-17
 */
@Api(tags = "资源相关接口")
@RestController
@RequestMapping("/admin/ums/resource")
@Slf4j
public class UmsResourceController {
    @Resource
    private IUmsResourceService umsResourceService;

    @PreAuthorize(value = "hasAuthority('admin:ums:resource')")
    @ApiOperation(value = "资源分页查询", produces = "application/json")
    @PostMapping(value = "/page")
    public BaseResDTO<BasePageResDTO<List<UmsResourcePageResDTO>>> page(@Validated @RequestBody UmsResourcePageReqDTO reqDTO) {
        log.info("资源分页查询,入参:{}", JSON.toJSONString(reqDTO));
        BasePageResDTO<List<UmsResourcePageResDTO>> resDTO = umsResourceService.pageResource(reqDTO);
        return ResponseUtils.ok(resDTO);
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:resource')")
    @ApiOperation(value = "资源新增", produces = "application/json")
    @PostMapping(value = "/save")
    public BaseResDTO<Boolean> save(@Validated @RequestBody UmsResourceSaveReqDTO reqDTO) {
        log.info("资源新增,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsResourceService.saveResource(reqDTO);
        log.info("资源新增,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:resource')")
    @ApiOperation(value = "资源删除", produces = "application/json")
    @PostMapping(value = "/remove")
    public BaseResDTO<Boolean> remove(@Validated @RequestBody UmsResourceReqDTO reqDTO) {
        log.info("资源删除,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsResourceService.removeResource(reqDTO);
        log.info("资源删除,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

    @PreAuthorize(value = "hasAuthority('admin:ums:resource')")
    @ApiOperation(value = "资源更新", produces = "application/json")
    @PostMapping(value = "/modify")
    public BaseResDTO<Boolean> modify(@Validated @RequestBody UmsResourceModifyReqDTO reqDTO) {
        log.info("资源更新,入参:{}", JSON.toJSONString(reqDTO));
        boolean resDTO = umsResourceService.modifyResource(reqDTO);
        log.info("资源更新,返参:{}", JSON.toJSONString(resDTO));
        return ResponseUtils.ok();
    }

}

